package algorithm.difficult;

import pojo.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一棵二叉树的根节点 root ，请你返回 层数最深的叶子节点的和 。
 * <a href="https://leetcode.cn/problems/deepest-leaves-sum/">...</a>
 */
public class DeepestLeavesSum1302 {

    int res;

    public int deepestLeavesSum(TreeNode root) {
        return bfs(new ArrayList<TreeNode>() {{
            add(root);
        }});
    }

    public int bfs(List<TreeNode> list) {
        ArrayList<TreeNode> temp = new ArrayList<>();
        res = 0;
        for (TreeNode treeNode : list) {
            if (treeNode.left != null) {
                temp.add(treeNode.left);
            }
            if (treeNode.right != null) {
                temp.add(treeNode.right);
            }
            res += treeNode.val;
        }
        if (temp.isEmpty()) {
            return res;
        }
        return bfs(temp);
    }
}
